﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using XiaoLiuren.Annotations;

namespace XiaoLiuren
{
    public class MainViewModel : BindableBase
    {
        private ChineseLunisolarCalendar cncld = new ChineseLunisolarCalendar();

        #region Property
        private string startTime = "";

        public string StartTime
        {
            get { return startTime; }
            set { SetProperty(ref startTime, value); }
        }

        private int nongLiMon;

        public int NongLiMon
        {
            get { return nongLiMon; }
            set { SetProperty(ref nongLiMon, value); }
        }

        private int nongLiDay;

        public int NongLiDay
        {
            get { return nongLiDay; }

            set { SetProperty(ref nongLiDay, value); }
        }

        private int nongLiTime;

        public int NongLiTime
        {
            get { return nongLiTime; }
            set { SetProperty(ref nongLiTime, value); }
        }

        private GuaXiang indexGua = GuaXiang.大安;

        public GuaXiang IndexGua
        {
            get { return indexGua; }
            set { SetProperty(ref indexGua, value); }
        }

        private int indexGuaNum = 1;

        public int IndexGuaNum
        {
            get { return indexGuaNum; }
            set { SetProperty(ref indexGuaNum, value); }
        }


        private string rtYue = "";

        public string RtYue
        {
            get { return rtYue; }
            set { SetProperty(ref rtYue, value); }
        }

        private string rtRi = "";

        public string RtRi
        {
            get { return rtRi; }
            set { SetProperty(ref rtRi, value); }
        }

        private string rtShi = "";

        public string RtShi
        {
            get { return rtShi; }
            set { SetProperty(ref rtShi, value); }
        }

        private bool useSysTime = true;

        public bool UseSysTime
        {
            get { return useSysTime; }
            set { SetProperty(ref useSysTime, value); }
        }

        private string year = "";

        public string Year
        {
            get { return year; }
            set { SetProperty(ref year, value); }
        }

        private string month = "";

        public string Month
        {
            get { return month; }
            set { SetProperty(ref month, value); }
        }

        private string day = "";

        public string Day
        {
            get { return day; }
            set { SetProperty(ref day, value); }
        }

        private string hour = "";

        public string Hour
        {
            get { return hour; }
            set { SetProperty(ref hour, value); }
        }
        private string minute = "";

        public string Minute
        {
            get { return minute; }
            set { SetProperty(ref minute, value); }
        }

        private bool useRandom = false;

        public bool UseRandom
        {
            get { return useRandom; }
            set { SetProperty(ref useRandom, value); }
        }

        private string random1 = "";

        public string Random1
        {
            get { return random1; }
            set { SetProperty(ref random1, value); }
        }

        private string random2 = "";

        public string Random2
        {
            get { return random2; }
            set { SetProperty(ref random2, value); }
        }

        private string random3 = "";

        public string Random3
        {
            get { return random3; }
            set { SetProperty(ref random3, value); }
        }

        private bool noRunning = true;

        public bool NoRunning
        {
            get { return noRunning; }
            set { SetProperty(ref noRunning, value); }
        }

        private string jieguaText = "";

        public string JieguaText
        {
            get { return jieguaText; }
            set { SetProperty(ref jieguaText, value); }
        }

        #endregion

        #region Command

        private ICommand start = null;

        public ICommand StartCommand
        {
            get
            {
                if (start == null)
                {
                    start = new RelayCommand(StartFunc);
                }

                return start;
            }
        }

        private ICommand screachCommand;

        public ICommand ScreachCommand
        {
            get
            {
                if (screachCommand == null)
                {
                    screachCommand=new RelayCommand(SearchFunc);
                }

                return screachCommand;
            }
        }

        #endregion


        #region build

        public MainViewModel()
        {

        }

        #endregion


        #region func

        private void StartFunc()
        {
            if (!UseRandom)
            {
                DateTime dt;
                if (UseSysTime)
                {
                    dt = DateTime.Now;
                    Year = dt.Year.ToString();
                    Month = dt.Month.ToString();
                    Day = dt.Day.ToString();
                    Hour = dt.Hour.ToString();
                    Minute = dt.Minute.ToString();
                }
                else
                {
                    string str = $"{Year}-{Month}-{Day} {Hour}:{Minute}:00";

                    if (!DateTime.TryParse(str, out dt))
                    {
                        MessageBox.Show("填写日期时间有误!");
                        return;
                    }
                }

                IndexGua = GuaXiang.大安;
                RtYue = "";
                RtRi = "";
                RtShi = "";
                JieguaText = "";
                int year = cncld.GetYear(dt);
                // 是否有闰月,返回正整数（2020年闰4月，返回值为5）
                int flag = cncld.GetLeapMonth(year);
                NongLiMon = flag > 0 ? cncld.GetMonth(dt) - 1 : cncld.GetMonth(dt);
                NongLiDay = cncld.GetDayOfMonth(dt);
                NongLiTime = Tools.ConvertTime2ShiChen(dt);
            }
            else
            {
                if (!string.IsNullOrEmpty(Random1) && !string.IsNullOrEmpty(Random2) && !string.IsNullOrEmpty(Random3))
                {
                    if (Regex.IsMatch(Random1, "^[1-9]\\d*$") &&
                        Regex.IsMatch(Random2, "^[1-9]\\d*$") &&
                        Regex.IsMatch(Random3, "^[1-9]\\d*$"))
                    {
                        NongLiMon =int.Parse(Random1);
                        NongLiDay = int.Parse(Random2);
                        NongLiTime = int.Parse(Random3);
                    }
                    else
                    {
                        MessageBox.Show("输入的数字不正确");
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("填入三个数字");
                    return;
                }
            }

            Yue();
        }

        /// <summary>
        /// 月份起局
        /// </summary>
        private void Yue()
        {
            //IndexGua +=(NongLiMon - 1);
            NoRunning = false;
            int cnt = NongLiMon - 1;

            Task.Run(() =>
            {
                for (int i = 0; i < cnt; i++)
                {
                    IndexGuaNum += 1;
                    App.Current.Dispatcher.Invoke(() => { IndexGua =(GuaXiang)GetRt(IndexGuaNum); });

                    Thread.Sleep(50);
                }
            }).ContinueWith(x =>
            {
                int guax = GetRt(IndexGuaNum);

                RtYue = EnumDic.GuaXiangDic[guax];

                JieguaText = EnumDic.Remask[RtYue];

                Ri();
            });
        }
        
        private void Ri()
        {
            int cnt = NongLiDay - 1;

            Task.Run(() =>
            {
                for (int i = 0; i < cnt; i++)
                {
                    IndexGuaNum += 1;
                    App.Current.Dispatcher.Invoke(() => { IndexGua = (GuaXiang)GetRt(IndexGuaNum); });

                    Thread.Sleep(50);
                }
            }).ContinueWith(x =>
            {
                int guax = GetRt(IndexGuaNum);

                RtRi = EnumDic.GuaXiangDic[guax];
                JieguaText = $"{JieguaText} \r\n{EnumDic.Remask[RtRi]}";
                Shi();
            });
        }
        private void Shi()
        {
            int cnt = NongLiTime - 1;

            Task.Run(() =>
            {
                for (int i = 0; i < cnt; i++)
                {
                    IndexGuaNum += 1;
                    App.Current.Dispatcher.Invoke(() => { IndexGua = (GuaXiang)GetRt(IndexGuaNum); });

                    Thread.Sleep(50);
                }
            }).ContinueWith(x =>
            {
                int guax = GetRt(IndexGuaNum);

                RtShi = EnumDic.GuaXiangDic[guax];
                JieguaText = $"{JieguaText} \r\n{EnumDic.Remask[RtShi]}";
                NoRunning = true;
            });
        }
        
        private int GetRt(int num)
        {
            if (num <= 6)
            {
                return num;
            }

            int ys = num % 6;

            if (ys == 0)
            {
                return 6;
            }
            else
            {
                return ys;
            }
        }

        private void SearchFunc()
        {
            if (string.IsNullOrEmpty(rtRi) || string.IsNullOrEmpty(rtShi))
            {
                return;
            }

            string k = $"{RtRi}加{RtShi}";

            if (EnumDic.XunWuGuaCi.ContainsKey(k))
            {
                MessageBox.Show(EnumDic.XunWuGuaCi[k]);
            }
            else
            {
                MessageBox.Show(k);
            }
        }


        #endregion

    }
}
